package com.example.jyjava.controller;

import com.alibaba.fastjson.JSON;
import com.example.jyjava.bean.QueryInfo;
import com.example.jyjava.bean.Tab;
import com.example.jyjava.bean.User;
import com.example.jyjava.dao.TabDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class TabController {
    @Autowired
    private TabDao tabDao;

    //商品导航
    @RequestMapping("/tab")
    public String tab(){
        String falg = "400";
        List<Tab> tabList = tabDao.getAllTab();
        HashMap<String, Object> res = new HashMap<>();
        if (tabList != null){
            falg = "200";
            res.put("flag",falg);
            res.put("tab",tabList);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/allTab")
    public String getTabList(QueryInfo queryInfo){
        String falg = "400";
        //获取最大列表和当前编号
        int numbers = tabDao.getTabCounts("%" + queryInfo.getQuery() + "%");
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Tab> tb = tabDao.getTab("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();

        if (tb != null){
            falg = "200";
            res.put("flag",falg);
            res.put("number",numbers);
            res.put("data",tb);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/addTab")
    public String addUser(@RequestBody Tab tab){
        String falg = "400";
        tab.setTabState(false);
        int i = tabDao.addTab(tab);
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("tab",i);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/tabState")
    public String updateUserState(@RequestParam Integer id,
                                  @RequestParam Boolean tabState){
        String falg = "400";
        int i = tabDao.updateState(id,tabState);
        HashMap<String, Object> res = new HashMap<>();

        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("tab",i);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/getUpdateTab")
    public String getUpdateUser(int id){
        String falg = "400";
        Tab tab = tabDao.getUpdateTab(id);
        HashMap<String, Object> res = new HashMap<>();

        if (tab != null){
            falg = "200";
            res.put("flag",falg);
            res.put("tab",tab);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/editTab")
    public String editUser(@RequestBody Tab tab){
        String falg = "400";
        int i = tabDao.editTab(tab);
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("tab",i);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/deleteTab")
    public String deleteUser(int id){
        String falg = "400";
        int i = tabDao.deleteTab(id);
        HashMap<String, Object> res = new HashMap<>();

        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("tab",i);
        }else{
            res.put("flag",falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }
}
